package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.StatDb;


public class StatDbManager extends HibernateDaoSupport {

	public void addStatDb(StatDb statDb) {
		
		getHibernateTemplate().save(statDb);
	}
	
	public void updateStatDb(StatDb statDb) {
		
		getHibernateTemplate().update(statDb);
	}
	
	public void deleteStatDb(int id){
		
		getHibernateTemplate().delete(getStatDb(id));
	}
	
	public StatDb getStatDb(int id){
		StatDb d =(StatDb)getHibernateTemplate().get(StatDb.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<StatDb> listStatDb() {
		return getHibernateTemplate().find("from StatDb");
	}

	
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerStatDb(int start,int limit,int id) {
		List <StatDb>list = getHibernateTemplate().find("from StatDb i  where STAT_ID = " + id );
		StatDb s = new StatDb();
		s.setCode("合计");
		for (StatDb statDb : list) {
			s.setMoney(s.getMoney() + statDb.getMoney());
			s.setTax(s.getTax() + statDb.getTax());
		}
		list.add(s);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <StatDb>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



